export default {
  data() {
    return {
      countdown: 0, // 倒计时秒数
      timer: null, // 定时器对象
      totalTime: 60, // 总倒计时时间
      isShowPasAgain: false, //显示密码
      isShowPas: false, //显示密码
      params: {
        phone: '', //手机号码
        passwordAgain: '', //再次输入密码
        password: '', //密码
        username: '', //用户名
        verifyCode: '' //验证码
      }
    };
  },
  computed: {
    verifyLabel() {
      return this.countdown > 0 ? `${this.countdown} 秒后重发` : '发送验证码';
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    //显示密码框
    showPassword(fieldName) {
      this[fieldName] = !this[fieldName];
    },
    //登录
    register() {
      let { phone, passwordAgain, password, verifyCode } = this.params;
      let reg = /^1(3[0-9]|4[5-9]|5[0-35-9]|6[567]()|7[0-8]|8[0-9]|9[0-9])\d{8}$/;
      if (!reg.test(phone)) {
        return this.$message.warning('请输入正确手机号');
      }
      if (!verifyCode) {
        return this.$message.warning('请输入验证码');
      }
      if (!password) {
        return this.$message.warning('请输入密码');
      } else if (this.$rules.isValidPassword(password) == false) {
        return this.$message.warning('密码只能是字母数字特殊字符大小写四选三');
      }
      if (!passwordAgain) {
        return this.$message.warning('请再次输入密码');
      }
      if (passwordAgain !== password) {
        return this.$message.warning('两次密码不一致');
      }
      this.params = {
        verifyCode,
        phoneNumber: phone,
        newPassword: password
      };
      this.$api.post(this.$url.resetPassword, this.params).then(response => {
        if (response.code == 200) {
          this.$message.success('密码修改成功');
          this.goPage('login');
        }
      });
    },
    goPage(pageUrl) {
      this.$store.commit('setToken', '');
      window.localStorage.clear();
      window.sessionStorage.clear();
      this.$router.push({ path: '/' + pageUrl });
    },
    //发送验证码
    sendCode() {
      let reg = /^1(3[0-9]|4[5-9]|5[0-35-9]|6[567]()|7[0-8]|8[0-9]|9[0-9])\d{8}$/;
      if (!reg.test(this.params.phone)) {
        return this.$message.warning('请输入正确手机号');
      }
      if (this.countdown === 0) {
        this.$api.post(this.$url.sendVerifyCode, { phoneNumber: this.params.phone, type: '2' }).then(response => {
          if (response.code == 200) {
            this.countdown = this.totalTime;
            this.timer = setInterval(() => {
              if (this.countdown > 0) {
                this.countdown--;
              } else {
                clearInterval(this.timer);
                this.timer = null;
              }
            }, 1000);
          }
        });
      }
    }
  }
};
